<template>
	<div>
		<CarInfo
			ref="CarInfo"
			v-for="(item, index) in infoData.productList"
			v-model="infoData.productList[index]"
			:state="state"
			:index="index + 1"
			:type="type"
			:planDicts="planDicts"
			:cityList="cityList"
			:insuranceList="insuranceList"></CarInfo>
	</div>
</template>

<script>
	import CarInfo from '@/views/quota/forecast/chunk/carInfo';
	import * as apis from '@/api/quota/forecast/edit/api.js';
	import { regionList } from '@/api/system/dept.js';
	export default {
		components: { CarInfo },
		props: {
			value: {
				required: true,
			},
			state: {
				required: false,
			},
			type: {
				required: false,
				type: String,
			},
			cityList: {
				required: false,
			},
			planDicts: {
				required: false,
			},
		},
		data() {
			return {
				// planDicts: {
				// 	levelList: [],
				// 	packagelList: [],
				// },
				// cityList: [],
				insuranceList: [],
			};
		},
		computed: {
			infoData: {
				get() {
					return this.value;
					// return this.useVModel.call(this);
				},
				set(val) {
					this.$emit('input', val);
				},
			},
		},
		// provide() {
		//     return {
		//         _planDicts: () => this.planDicts,
		//         _cityList: () => this.cityList,
		//         _insuranceList: () => this.insuranceList,
		//     };
		// },
		created() {
			this.infoData.productList ??= [];
			// this.getRegionList();
			// this.getInsuranceList();
		},
		mounted() {},
		methods: {
			computeAll() {
				return new Promise(async (res, rej) => {
					let CarInfos = this.$refs.CarInfo;
					let index = 0;
					try {
						for (; index < CarInfos.length; index++) {
							const element = CarInfos[index];
							await element.compute();
						}

						res(true);
					} catch (error) {
						console.log(error);
						rej(index);
					}
				});
			},
			// 商业险险种
			getInsuranceList() {
				apis.Insurance({}).then(res => {
					if (res.code == 200) {
						this.insuranceList = res.data;
					}
				});
			},

			valid() {
				return new Promise(async resolve => {
					let result = true;
					let refs = Object.values(this.$refs);
					for (let index = 0; index < refs?.length; index++) {
						const ref = refs[index];
						if (
							Object.prototype.toString.call(ref) ==
							'[object Array]'
						) {
							for (let i = 0; i < ref.length; i++) {
								const element = ref[i];
								if (element?.valid) {
									let rc = await element?.valid?.();
									result = result && rc;
								}
								if (element?.validate) {
									let rf = await this._valid(ref);
									result = result && rf;
								}
								if (element?.form_validate) {
									let rf = await this._valid(
										ref,
										'form_validate',
									);
									result = result && rf;
								}
							}
						} else {
							if (ref?.valid) {
								let rc = await ref?.valid?.();
								result = result && rc;
							}
							if (ref?.validate) {
								let rf = await this._valid(ref);
								result = result && rf;
							}
							if (ref?.form_validate) {
								let rf = await this._valid(
									ref,
									'form_validate',
								);
								result = result && rf;
							}
						}
					}

					resolve(result);
				});
			},
		},
	};
</script>

<style lang="scss" scoped></style>
